package java.com.fitness.club.main;

import com.fitness.club.util.DataStringUtil;
import com.fitness.club.util.FreemarkerUtil;
import freemarker.core.ParseException;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;

import java.com.fitness.club.model.TableInfo;
import java.com.fitness.club.util.DatabaseUtil;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
/**
 * 代码生成器
 * @author Administrator
 *
 */
public class Main {

	public static void main(String[] args) throws SQLException, TemplateNotFoundException,
			MalformedTemplateNameException, ParseException, IOException, TemplateException {
		FreemarkerUtil freemarkerUtil = new FreemarkerUtil();
		List<TableInfo> tables = DatabaseUtil.getTableInfo(DatabaseUtil.databaseConfig.getTableNames());
		String entityPath = "";
		String mapperPath = "";
		String providerPath="";
		String queryPath="";
		String srcPath=System.getProperty("user.dir");
		for (TableInfo table : tables) {

			System.out.println("正在生成表【"+table.getDbTableName()+"】文件");
			entityPath = DataStringUtil.getEntityPath(table.getTableName(), DatabaseUtil.databaseConfig.getSavePath());

			mapperPath = DataStringUtil.getMapperPath(table.getTableName(), DatabaseUtil.databaseConfig.getSavePath());
			
			providerPath=DataStringUtil.getProviderPath(table.getTableName(), DatabaseUtil.databaseConfig.getSavePath());
			
			queryPath=DataStringUtil.getQueryPath(table.getTableName(), DatabaseUtil.databaseConfig.getSavePath());
			
			/**
			 * freemarker 根据模板创建Entity对象
			 * 
			 */
			freemarkerUtil.createFileByTempalte(table, "templates/entity.ftl", entityPath, DatabaseUtil.databaseConfig);
		
			/**
			 * freemarker 根据模块创建Mapper文件
			 * 
			 */
			freemarkerUtil.createFileByTempalte(table, "templates/mapper.ftl", mapperPath, DatabaseUtil.databaseConfig);
		
			/**
			 * freemarker 根据模板创建Provider文件
			 * 
			 */
			if (DatabaseUtil.databaseConfig.getType().equals("mysql")) {
				freemarkerUtil.createFileByTempalte(table, "templates/mysql-provider.ftl", providerPath, DatabaseUtil.databaseConfig);
			}else if (DatabaseUtil.databaseConfig.getType().equals("mssql")){
				freemarkerUtil.createFileByTempalte(table, "templates/mssql-provider.ftl", providerPath, DatabaseUtil.databaseConfig);
			}
			
			/**
			 * freemarker 根据模板创建Query文件
			 * 
			 */
			freemarkerUtil.createFileByTempalte(table, "templates/query.ftl", queryPath, DatabaseUtil.databaseConfig);
			
		}
	}

}
